package servlet;

import bean.Good;
import dao.entity.GoodDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "goodQueryByTypeServlet", value = "/gquerybt")
public class GoodQueryByTypeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取数
        String type = request.getParameter("type");
        String gid = request.getParameter("gid");
        String page = request.getParameter("currentPage");
        int currentPage = 1;//默认的当前页码为1
        if(page!=null){
            currentPage= Integer.parseInt(page);
            request.setAttribute("currentPage",currentPage);
        }
        GoodDaoImpl gdi = new GoodDaoImpl();//创建实现类的对象
        List<Good> list = null;
        list = gdi.good_findByType(currentPage,type);

        int n = gdi.good_count();//记录数
        int pages = (int) Math.ceil(n / 5.0);//计算页数,向上取整4.1-->5.0
        request.setAttribute("gid", gid);
        request.setAttribute("list", list);
        request.setAttribute("pages", pages);
        request.getRequestDispatcher("good.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
